package main


import (
	"github.com/streadway/amqp"
	"log"
	"os"
	"strings"
)

func failOnError(err error, msg string){
	if err != nil{
		log.Fatalf("%s: %s", msg, err)
	}
}



func main() {
	conn, err := amqp.Dial("amqp://guest:guest@172.168.0.100:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.ExchangeDeclare("log", "fanout", true,false, false, false, nil)
	failOnError(err, "Failed to declare a queue")

	body := bodyFrom(os.Args)
	err = ch.Publish("logs", "", false, false, amqp.Publishing{
		ContentType: "text/plain", Body: []byte(body)})
	failOnError(err, "Failed to publish a message")
	log.Printf(" [x] sent %s", body)
}


func bodyFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == ""{
		s= "hello"
	}else {
		s = strings.Join(args[1:], " ")
	}
	return s
}